import { createRouter, createWebHashHistory } from 'vue-router'
import { constantRoutes } from './routes'
import Layout from '@/layout/index.vue'

const routes: any = [
  ...constantRoutes,
  {
    path: '/',
    redirect: '/home',
  },
  {
    path: '/home',
    name: 'home',
    meta: { title: '首页', requireAuth: false },
    component: Layout,
    chilren: [
      {
        path: '/home',
        name: 'home',
        meta: { title: '首页', requireAuth: false },
        component: () => import('@/views/home/index.vue'),
      },
    ],
  },
  {
    path: '/login',
    name: 'login',
    meta: { title: 'login', requireAuth: false },
    component: () => import('@/views/login/index.vue'),
  },
  {
    path: '/404',
    name: '404',
    meta: { title: '404', requireAuth: true, hidden: true },
    component: () => import('@/views/404/index.vue'),
  },
  {
    path: '/:pathMatch(.*)',
    redirect: '/404',
    name: 'Any',
  },
]

const router = createRouter({
  // history: import.meta.env.VITE_ROUTER_MODE === 'history' ? 'history' : 'hash',
  history: createWebHashHistory(),
  routes,
  //   滚动行为
  scrollBehavior: () => ({ left: 0, top: 0 }),
})

// 动态添加路由
// routes.forEach(route => {
//   console.log(route, 'll');
//   const f = router.hasRoute(route)
//   console.log(f, 'f');

//   router.addRoute(route); // 添加到根路由下
// });
const aa = router.getRoutes()
console.log(aa, 'aa')

export default router
